package mk.rpg
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mk.game.key.KeyController;
	import mk.gamemap.model.Director;
	import mk.gamemap.model.TileMapModel0;
	import game.tileMap.SpriteCache;
	import mk.gamemap.view.TileMapView;
	import mk.gamemap.view.sprite.CharView;
	import mk.rpg.view.MessageWindow;

	public class World
	{
		public var gameStage:Sprite;
		public var running:Boolean;
		public var mapView:TileMapView
		public var player:CharView
		public var director:Director
		private var spFactory:SpriteCache;
		public var worldModelBox:WorldModelBox
		public var uiManager:UIManager;
		
		public var keyCtrl:KeyController
		
		public function World(gameStage:Sprite)
		{
			spFactory=new SpriteCache();
			this.gameStage=gameStage
			init()
			
		}
		
		private function init():void{
		//	spriteList=[];
			worldModelBox=new WorldModelBox();
			mapView=new TileMapView(worldModelBox.mapModel);
			gameStage.addChild(mapView.canvas);
				
			director=new Director(this);
			
			director.mapModel=worldModelBox.mapModel;
			director.setup();
			
			
			mapView.drawMap();
			this.player=mapView.player
			director.player=player;
			gameStage.addEventListener(Event.ENTER_FRAME,step);
			
			uiManager=new UIManager(gameStage);
			
			uiManager.msgWindow.addEventListener(MouseEvent.CLICK,clickMsgWindow);
			
			running=true;
			
		}
		
		public function setPos(x:int,y:int):void{
			player.X=x;
			player.Y=y;
			player.sp.x=x*32
			player.sp.y=y*32
		}
		
		private function clickMsgWindow(event:Event):void{
			running=true;
			//director.changeMap("2")
			director.running=false;
			keyCtrl.isCtrl=true;
			uiManager.msgWindow.nextPage();
			//			
		}
		
		private var  stepCount:int=0
		
		private function step(event:Event):void{
			if (running){
//				for each(var actor:Actor in spriteList){
//					actor.step();
//					
//				}
//				surround.step();
				director.update();
			mapView.update();
			
			//director.checkEvent();
				
			}
			
			
//			
		}
		
	}
}